package com.jinken._07tag;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.Arrays;
import java.util.List;

/**
 * 生产者
 */
public class Producer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //创建生产者对象
        DefaultMQProducer dmp = new DefaultMQProducer("tag_group");
        //设置NamseServer地址
        dmp.setNamesrvAddr("192.168.126.100:9876");
        //启动生产者
        dmp.start();

        //创建标签
        List<String> tags = Arrays.asList("a", "b", "c", "c", "d", "d", "b");
        for (String tag : tags) {
            //创建消息对象
            Message msg = new Message("tag_topic", tag, ("消息" + tag).getBytes());
            //发送消息
            SendResult result = dmp.send(msg);
            System.out.println("result = " + result);
        }


        //关闭生产者
        dmp.shutdown();
    }

}
